<?php
//echo $cantmd;
$container_bl=join('&nbsp;', $form->_button_container['BL']);
$container_br=join('&nbsp;', $form->_button_container['BR']);
$container_tr=join('&nbsp;', $form->_button_container['TR']);

$campos=$form->template_details('itbmov');
$scampos  ='<tr id="tr_itbmov_<#i#>">';
$scampos .='<td class="littletablerow" align="left" >'.$campos['docu']['field'].'</td>';
$scampos .='<td class="littletablerow" align="left" >'.$campos['cuenta']['field'].'</td>';
$scampos .='<td class="littletablerow" align="left" >'.$campos['desca']['field'].'</td>';
$scampos .='<td class="littletablerow" align="left" >'.$campos['ruc']['field'].'</td>';
$scampos .='<td class="littletablerow" align="left" >'.$campos['tipo']['field'].'</td>';
$scampos .='<td class="littletablerow" align="right">'.$campos['importe']['field'].'</td>';
$scampos .='<td class="littletablerow" align="right">'.$campos['creditos']['field'];

$scampos .= '<td class="littletablerow"><a href=# onclick="del_itbmov(<#i#>);return false;">'.img("images/delete.jpg").'</a></td></tr>';
$campos=$form->js_escape($scampos);

if ($form->_status=='delete' || $form->_action=='delete' || $form->_status=='unknow_record'):
	echo $form->output;
else:

if(isset($form->error_string)) echo '<div class="alert">'.$form->error_string.'</div>';
echo $form_scripts;
echo $form_begin;


$link=base_url()."common/numegreso";
$link2=base_url()."common/numrecibo";
$link4=base_url()."common/moneda";
$link3=base_url()."common/cfecha";

if($form->_status!='show'){ ?>

<script language="javascript" type="text/javascript">
var itbmov_cont=<?php echo $form->max_rel_count['itbmov']; ?>;

$(function(){
	$(".inputnum").numeric(".");
	//totalizar();
});
function add_itbmov(){
	var htm = <?php echo $campos; ?>;
	can = itbmov_cont.toString();
	con = (itbmov_cont+1).toString();
	htm = htm.replace(/<#i#>/g,can);
	htm = htm.replace(/<#o#>/g,con);
	$("#__INPL__").after(htm);
	$("#cana_"+can).numeric(".");
	$('#cuenta_'+can).focus();
	itbmov_cont=itbmov_cont+1;
}
function del_itbmov(id){
	id = id.toString();
	$('#tr_itbmov_'+id).remove();
	
	totalizar3();
}
function manual1(){
	
	var cambio  = $("#cambio").val();
	var smonto  = $("#smonto").val();
	
	$("#dmonto").val(smonto/cambio);
	
	
}
function manual2(){
	
	var cambio  = $("#cambio").val();
	var dmonto  = $("#dmonto").val();
	
	$("#smonto").val(dmonto*cambio);


}
function cfecha(){
	
	fecha    = $("#fecha").val();

	$.ajax({
		url: "'.$link3.'"+"/"+fecha,
		success: function(msg){
			if(msg){
				alert("Valor de la moneda "+msg+" de FECHA "+fecha);
				$("#cambio").val(msg);
			} else {
				alert("Registrar el valor de la moneda para la FECHA "+fecha);
				$("#cambio").val(0);
			}
		}
	});

}
function tcambio(){

	monto2    = $("#monto2").val();
	monto    = $("#monto").val();
	
	var total =roundNumber(monto/monto2,3);
	$("#cambio").val(total);

}
function mcambio(){

	banco     = $("#banco").val();
	banco2    = $("#banco2").val();
	monto     = $("#monto").val();	
	cambio    = $("#cambio").val();
	formap    = $("#clipro").val();
	
	if(formap=="P"){
	
		if((banco=="003")||(banco=="005")||(banco=="008")||(banco=="010")){	
			moneda1="02";
		}else{	
			moneda1="01";		
		}
		
		if((banco2=="003")||(banco2=="005")||(banco2=="008")||(banco=="010")){	
			moneda2="02";
		}else{	
			moneda2="01";		
		}
		
		if(moneda1==moneda2){
			
				tmonto2    = monto;
		
		}else{
			
			if(moneda1=="01"){
				tmonto2    = monto/cambio;
			}else{
				tmonto2    = monto*cambio;
			}
		}
		
		var total =roundNumber(tmonto2,2);
		
	}else{
	   $("#banco2").val("");
		var total=0;
	
	}
	
	$("#monto2").val(total);
}
function tipod(){
	clipro    = $("#clipro").val();
	if(clipro=="P"){
		alert("Ultimo numero de comprobante para EGRESOS")
		numegreso();
	}else{
		alert("Ultimo numero de comprobante para INGRESOS")
		numrecibo();
	}
	mcambio();

}
function numegreso(){
	
	seriei    = $("#banco").val();
	$.ajax({
		url: "<?=$link?>"+'/'+seriei,
		success: function(msg){
			if(msg){
				$("#numero").val(msg);
			} else {
				alert("No es posible generar un numero");
			}
		}
	});
	
	moneda(seriei);
}
function numrecibo(){

	seriei    = $("#banco").val();
	$.ajax({
		url: "<?=$link2?>"+'/'+seriei,
		success: function(msg){
			if(msg){
				$("#numero").val(msg);
			} else {
				alert("No es posible generar un numero");
			}
		}
	});
	
	moneda(seriei);
}
function importe(id){
	
	var ind     = id.toString();
	var vcreditos = $("#tipo_"+ind).val();
		
	importe2= Number($("#importe_"+ind).val());
	
	if(vcreditos=='D'){
		
		$("#creditos_"+ind).val(0);	
	
	}else{
	
		$("#creditos_"+ind).val(importe2);
	
	}
	
	totalizar3();
}
function cfecha(){
	
	fecha    = $("#fecha").val();

	$.ajax({
		url: "<?=$link3?>"+'/'+fecha,
		success: function(msg){
			if(msg){
				alert("Valor de la moneda "+msg+" de FECHA "+fecha);
				$("#cambio").val(msg);
			} else {
				alert("Registrar el valor de la moneda para la FECHA "+fecha);
				$("#cambio").val(0);
			}
		}
	});
	
	//total_manual();

}
function moneda(seriei){
	
	$.ajax({
		url: "<?=$link4?>"+'/'+seriei,
		success: function(msg){
			if(msg){
				$("#moneda").val(msg);
			} else {
				alert("No es posible verificar la moneda");
			}
		}
	});
	
	//total_manual();

}
function totalizar3(){
	
	var tdebitos =0;
	var tcreditos =0;
	var importe=0;
	var creditos=0;
	var ttotals=0;
	var ttotald=0;
	
	var cambio  = $("#cambio").val();
	var monedab = $("#moneda").val();
	var clipro  = $("#clipro").val();
	
	var arr=$('input[name^="importe_"]');
	jQuery.each(arr, function() {
		nom=this.name;
		pos=this.name.lastIndexOf('_');
		if(pos>0){
			importe = Number(this.value);	
			tdebitos  = (tdebitos+importe);
		}
	});
	
		
	var arr=$('input[name^="creditos_"]');
	jQuery.each(arr, function() {
		nom=this.name;
		pos=this.name.lastIndexOf('_');
		if(pos>0){
			creditos  = Number(this.value);	
			tcreditos = (tcreditos+creditos);
		}
	});
	
	var arr=$('input[id^="dolar_"]');
	jQuery.each(arr, function() {
		nom=this.id;
		pos=this.id.lastIndexOf('_');
		if(pos>0){
			dolar = Number(this.value);	
			ttotald  = (ttotald+dolar);
		}
	});
	
	var arr=$('input[id^="sol_"]');
	jQuery.each(arr, function() {
		nom=this.id;
		pos=this.id.lastIndexOf('_');
		if(pos>0){
			sol = Number(this.value);	
			ttotals  = (ttotals+sol);
		}
	});
    
	var resul=tdebitos-tcreditos;
		
	$("#tdebitos").val(resul);
	$("#tcreditos").val(tcreditos);
	
	ttdebitos = parseFloat($("#tdebitos").val());
	ttcreditos= parseFloat($("#tcreditos").val());
	
	var tot=ttdebitos-ttcreditos;
		
	if(monedab=='01'){
		var sin_provis=tot;
	}else{
		var sin_provis=tot*cambio;
	}
	
	if(monedab=='02'){
		var sin_provid=tot;
	}else{
		var sin_provid=tot/cambio;
	}
    
	var tgenerals=ttotals+sin_provis;
	var tgenerald=ttotald+sin_provid;
	
	if(clipro=='C'){
		
		tgenerald=(tgenerald*-1);
		tgenerals=(tgenerals*-1);
	}
	
	$("#dmonto").val(tgenerald);
	$("#smonto").val(tgenerals);
    //$("#smonto_val").text(nformat(tgenerals,2));
    //$("#dmonto_val").text(nformat(tgenerald,2));

}
</script>
<?php } ?>
<table align='center' width="90%" style='background: #EEEEEE'>
	<tr>
		<?php if ($form->_status=='show') { ?>
		<td>
		<a href="#" onClick="window.open('<?php echo base_url() ?>formatos/verhtml/TRANSF/<?php echo $form->id->value ?>', '_blank', 'width=800, height=600, scrollbars=Yes, status=Yes, resizable=Yes, screenx='+((screen.availWidth/2)-400)+',screeny='+((screen.availHeight/2)-300)+'');" heigth="600" >
		<img src='<?php echo base_url() ?>images/html_icon.gif'></a>
		<a href="#" onClick="window.open('<?php echo base_url() ?>formatos/ver/TRANSF/<?php echo $form->id->value ?>', '_blank', 'width=800, height=600, scrollbars=Yes, status=Yes, resizable=Yes, screenx='+((screen.availWidth/2)-400)+',screeny='+((screen.availHeight/2)-300)+'');" heigth="600" >
		<img src='<?php echo base_url() ?>images/pdf_logo.gif'></a> Comprobante 
		</td>
	<?php } ?>
	  <td colspan="3" align="right"><?php echo $container_tr?><?php echo $container_br?></td>
  </tr>
</table>
<table align='center' width="95%">
	<tr>
		<td>
         <table align='center' width="100%">
	<tr>
		<td>
		<table width='100%'>
			<tr><td width='50%'>
		<fieldset style='border: 2px outset #9AC8DA;background: #FFFFFF;'>
		<legend class="titulofieldset" style='color: #2E4B77;'>Informaci&ograve;n del Movimiento</legend>
			<table style="margin: 0; width: 100%;">
			<tr>
				<td width='40%' class="littletableheader"><?php echo $form->banco->label;    ?></td>
				<td class="littletablerow"><strong><?php echo $form->banco->output;   ?>-<?php echo $form->numero->output;   ?></strong></td>
			</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->clipro->label;    ?></td>
				<td class="littletablerow">   <?php echo $form->clipro->output;   ?></td>
			</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->fecha->label;    ?></td>
				<td class="littletablerow">   <?php echo $form->fecha->output;   ?></td>
			</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->ccosto->label;    ?></td>
				<td class="littletablerow">   <?php echo $form->ccosto->output;   ?></td>
			</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->cuenta2->label;    ?></td>
				<td class="littletablerow">   <?php echo $form->cuenta2->output;   ?></td>
			</tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->formap->label     ?></td>
			  <td class="littletablerow"><?php echo $form->formap->output    ?></td>
			</tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->referen->label     ?></td>
			  <td class="littletablerow"><?php echo $form->referen->output    ?></td>
			</tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->tipo2->label     ?></td>
			  <td class="littletablerow"><?php echo $form->tipo2->output    ?></td>
			  </tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->moneda->label     ?></td>
			  <td class="littletablerow"><?php echo $form->moneda->output    ?></td>
			</tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->cambio->label     ?></td>
			  <td class="littletablerow"><?php echo $form->cambio->output    ?></td>
			</tr>  
			</table>
			</fieldset>
		</td>
		<td>
			<fieldset style='border: 2px outset #9AC8DA;background: #FFFFFF;'>
			<legend  class="titulofieldset" style='color: #2E4B77;'>Identificaci&oacute;n del Proveedor </legend>
			<table  style="margin: 0; width: 100%;">
			<tr>
				<td width="33%" class="littletableheader"><?php echo $form->bproveed->label;  ?></td>
				<td class="littletablerow"><?php echo $form->bproveed->output; ?></td>
				</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->bnombre->label;  ?></td>
				<td class="littletablerow"><?php echo $form->bnombre->output;  ?></span></td>
			</tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->brif->label; ?></td>
			  <td class="littletablerow"><?php echo $form->brif->output;   ?></td>
			</tr>
			</table>
			</fieldset>
		</td>
		</tr>
		</table>
		</td>
	</tr>
	<tr>
		<td>
		<div style='overflow:auto;border: 1px solid #9AC8DA;background: #FAFAFA;height:120px'>
		<table width='85%' border='0' align="center">
			<tr id='__INPL__' class="littletablerow">
				<td bgcolor='#6DA7D3'>Documento</td>
				<td bgcolor='#6DA7D3'>Cuenta </td>
				<td bgcolor='#6DA7D3'>Descripci&oacute;n</td>
				<td bgcolor='#6DA7D3'>RUC</td>
				<td bgcolor='#6DA7D3'>Tipo</td>
				<td bgcolor='#6DA7D3' align="center">Monto</td>
				<?php if ($form->_status!='show') { ?>
				<td bgcolor='#6DA7D3' align="center">Creditos</td>
				<?}?>
				<?php if($form->_status!='show') {?>
					<td class="littletableheader">&nbsp;</td>
				<?php } ?>
			</tr>

			<?php for($i=0;$i<$form->max_rel_count['itbmov'];$i++) {
				$it_docu    = "docu_$i";
				$it_cuenta  = "cuenta_$i";
				$it_desca   = "desca_$i";
				$it_ruc     = "ruc_$i";
				$it_tipo    = "tipo_$i";
				$it_importe = "importe_$i";
				$it_creditos= "creditos_$i";
			

			?>

			<tr id='tr_itbmov_<?php echo $i; ?>'>
			    <td class="littletablerow" align="left"><?php echo $form->$it_docu->output; ?></td>
				<td class="littletablerow" align="left"><?php echo $form->$it_cuenta->output; ?></td>
				<td class="littletablerow" align="left"><?php echo $form->$it_desca->output;  ?></td>
				<td class="littletablerow" align="left"><?php echo $form->$it_ruc->output;  ?></td>
				<td class="littletablerow" align="center"><?php echo $form->$it_tipo->output;  ?></td>
				<td align="center" class="littletablerow"><?php echo $form->$it_importe->output;   ?></td>
				<?php if ($form->_status!='show') { ?>
				<td class="littletablerow" align="left"><?php echo $form->$it_creditos->output;?></td>
				<?php } ?>
				<?php if($form->_status!='show') {?>
				<td class="littletablerow">
					<a href='#' onclick='del_itbmov(<?=$i ?>);return false;'><?php echo img("images/delete.jpg"); ?></a>				</td>
				<?php } ?>
			</tr>
			<?php } ?>

			<tr id='__UTPL__'>
				<td id='cueca'></td>
			</tr>
		</table>
		</div>
		<?php echo $container_bl ?>
		</td>
	</tr>
	<tr>
		<td>
		<fieldset style='border: 2px outset #9AC8DA;background: #EFEFEF;'>
		<legend  class="titulofieldset" style='color: #2E4B77;' align="right">-- Totales --</legend>
		<table width='100%'>
	<?php if($form->_status!='show') {?>
				<tr>
				<td  width="15%" class="littletableheader"><?php echo $form->condi1->label;    ?></td>
				<td  width="25%" class="littletablerow"   ><?php echo $form->condi1->output;   ?></td>
				<td  width="40%" align='right' class="littletablerow"><strong><?php echo $form->monto->label;  ?></strong></td>
				<td  width="5%" align='right' class="littletablerow">S/.</td>
				<td  width="10%"><input id="smonto"  class="input"  name="smonto"  type="text" size="10" /></td>
				<td  width="5%" align='right' class="littletablerow">US$</td>
				<td  width="10%"><input id="dmonto"  class="input"  name="dmonto"  type="text" size="10" /></td>
				</tr>
			  <input type="hidden"    id="tcreditos"  value="0" name="tcreditos"   size="10" readonly="false" class="input" />
			  <input type="hidden"    id="tdebitos"   value="0" name="tdebitos"    size="10" readonly="false" class="input" />
        <?}else{?>
		<tr>
			  <td width='15%' class="littletableheader"><?php echo $form->condi1->label;    ?></td>
			  <td width='45%'class="littletablerow"   ><?php echo $form->condi1->output;   ?></td>
			  <td width='30%' align='right' class="littletablerow">Total</td>
			  <td width='5%' align='right' class="littletablerow">S/.</td>
			  <td width='5%' align='right' class="littletablerow"><?php echo $form->montos->output    ?></td>
		  </tr>
		  <tr>
			  <td width='15%' class="littletableheader"></td>
			  <td width='45%'class="littletablerow"   ></td>
			  <td width='30%' align='right' class="littletablerow">Total</td>
			  <td width='5%' align='right' class="littletablerow">US$</td>
			  <td width='5%' align='right' class="littletablerow"><?php echo $form->montod->output    ?></td>
		  </tr>
		<?}?>  
          </table>
		</fieldset>	
		</td>
	</tr>
</table>
<?php echo $form_end?>
<?php echo $container_br ?>
	  <td>
	<tr>
<table>
<?php endif; ?>
